package com.jdd.fintech.morse.plugin.util;

import com.jdd.fintech.morse.plugin.enums.BizErrorCodeEnum;
import com.jdd.fintech.morse.plugin.enums.CustomVersionEnum;
import com.jddglobal.filter.support.exception.FilterException;
import com.jddglobal.phevos.gateway.resources.ResourceCapability;
import com.wangyin.aks.security.sign.SignEnvelopService;
import com.wangyin.aks.security.sign.SignEnvelopServiceImpl;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RSAUtil {

    /**
     * 加密算法RSA
     */
    public static final String KEY_ALGORITHM = "RSA";
    /**
     * 获取公钥的key
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";
    /**
     * 获取私钥的key
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";
    /**
     * RSA签名算法
     */
    public static final String MD_5_WITH_RSA = "MD5withRSA";
    public static final String SHA_1_WITH_RSA = "SHA1withRSA";
    public static final String SHA_256_WITH_RSA = "SHA256withRSA";
    /**
     * 常量0
     */
    private static final int ZERO = 0;
    /**
     * RSA最大加密明文最大大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;
    /**
     * RSA最大解密密文最大大小
     * 当密钥位数为1024时,解密密文最大是 128
     * 当密钥位数为2048时需要改为 256 不然会报错（Decryption error）
     */
    private static final int MAX_DECRYPT_BLOCK = 256;
    /**
     * 默认key大小
     */
    private static final int DEFAULT_KEY_SIZE = 2048;
    /**
     * 公钥密钥map长度
     */
    private static final int MAP_SIZE = 2;
    /**
     * 公钥密钥map
     */
    private static final Map<String, Object> keyMap = new HashMap<String, Object>(MAP_SIZE);

    public static final Logger log = LoggerFactory.getLogger(RSAUtil.class);

    public static Map<CustomVersionEnum, SignEnvelopService> signCasheMap = new ConcurrentHashMap<>();

    //云工厂公钥、私钥、密码
    public static String saltygc = "123456";
    public static String ygcpublicSecurity = "MIICqDCCAhGgAwIBAgIJAJxaQ5hu03JPMA0GCSqGSIb3DQEBBQUAMG0xCzAJBgNVBAYTAmNuMQswCQYDVQQIDAJjZDELMAkGA1UEBwwCY2QxCzAJBgNVBAoMAnBwMQswCQYDVQQLDAJwcDELMAkGA1UEAwwCcHAxHTAbBgkqhkiG9w0BCQEWDmxpeHV3ZXVAamQuY29tMB4XDTIwMDMyNDA5NDYxM1oXDTQwMDMxOTA5NDYxM1owbTELMAkGA1UEBhMCY24xCzAJBgNVBAgMAmNkMQswCQYDVQQHDAJjZDELMAkGA1UECgwCcHAxCzAJBgNVBAsMAnBwMQswCQYDVQQDDAJwcDEdMBsGCSqGSIb3DQEJARYObGl4dXdldUBqZC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKjP/WDJhyk5f3QwPQZSJgYNObU7+tj9pMhXnO5HDQuVKRhiABQwS9fjc1Cwa/YlPA4QLp/pZhly2PqlqT73PeTDYvKZVzgY0krP7hnylXXmTWrr7Kim/L8cScJe5Y57OgyBxncoUOi/EhwRpu9YM/dx/nGuqhRwpfs7/hPSq1hRAgMBAAGjUDBOMB0GA1UdDgQWBBSsHzKYn5R13H2ufAiIFQV678NpZzAfBgNVHSMEGDAWgBSsHzKYn5R13H2ufAiIFQV678NpZzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBADC/R//9zV5VcgYry6HHxigoCeA9XBwHPRWnPng50nYi/QXSvDm0XMdN22IJy1SA23WT78yLyqdIZw/uXsfmbZNsTrkKJ3+TU/eY/OlVMAHvsWdiZjA1yICcwmsvznn+ZCCAxJOSIJAYwMyICdt7TgNp1Y9PcQg43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4nrlF2z4AYQGxZxLaqVC1n5FKZFLjAUp9fuVKg1PQ0Gnxj0SAdbwxRjAfBgkqhkiG9w0BCRQxEh4QAGMAYQBzAGgAbABvAGEAbjAjBgkqhkiG9w0BCRUxFgQU62tkq99HDXJs1ewTHKpWFyF8ip4wMTAhMAkGBSsOAwIaBQAEFCE7XIPOR6ViR+wFI4eF+OdcvbwGBAglINwWJqzJOAICCAA=";

    //网贷公钥、私钥、密码
    public static String saltwd = "PK4TF87tpKwhL7rm";
    public static String wdpublicSecurity = "MIIE6DCCA9CgAwIBAgIULW480ZR+VDIHvWGclgMCjjw9QOYwDQYJKoZIhvcNAQELBQAwgakxCzAJBgNVBAYTAkNOMTkwNwYDVQQKDDDljJfkuqzlpKnlqIHor5rkv6HnlLXlrZDllYbliqHmnI3liqHmnInpmZDlhazlj7gxIjAgBgNVBAsMGUpEcGF5LmNvbSBTZWN1cml0eSBDZW50ZXIxOzA5BgNVBAMMMuWMl+S6rOWkqeWogeivmuS/oeeUteWtkOWVhuWKoeacjeWKoeaciemZkOWFrOWPuENBMB4XDTIyMDQyNzAwNTAyN1oXDTIzMDQyNzAwNTAyN1owgYMxEzARBgNVBAsMCmpyIGNvbXBhbnkxDDAKBgNVBAoMA0pERDEfMB0GCSqGSIb3DQEJARYQbGl5dWFuMzYyQGpkLmNvbTE9MDsGA1UEAww05Lqs5Lic56eR5oqA5Z+656GA5a6J5YWo6YOo5rWL6K+V6K+B5LmmKEFLUzAwMDAxQUtTKTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2phYxDSqLsnJ7+XIZ16W0jxYxRYJnxXBWsMPulA+2MjtezsgsLQ1pI/TfqtGz84dxE+x0ybfIizRzh5kOtvUc01I0+noH2EpRZdZehnAo7Q8Ao6hVhDpbKfCHbkBp2MzrAG6GLBl9kQmt5n7a7FNyNDhTKAY26HVZR3Dx3wV+9+fwbgnPyd6Z8G5bkpayVZK9Tm3FSYcRytTI7SIFn7Dd8pGcc7XUfMM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6nv/DvlPZdq+C3I6An6qAYpjJ1Io2FSdA+TbaV4xuimPXgYFmJwUZEQ72EcEFa8BNwf/UgIK65Xw9a7s3XXxGzS6qf/VigzoU7iD8Tnn/VIjnXpUtAcjqUVE71kXhPRlkvvDEiSiQzqZaQvX1KW6njXfSA0AqGUBOgyk7u0nB6CcFcTIJgV7ZTPwWBI7dOkQb5aoSuvuk1y14J8b0UV5qGoFzJyopZooRzS9+5j66H+3YXbzDLLoF3x18+qvK0oqAVZd2GtVUlt60m9cpK+Y6X2XywDpJKHl3NJ3hVTs2ouHPsBZBtOBt6yTNWkwRbFFt0XZ5ZxJHDRZXoa16x3c1G61qUZWLT2o8h8a935kAFJ4qJ7Uzdx3wgMPhz2r/9ADPLwEc30qvNMpTpkWS7aVShmPscnB8yGdt044CWQvUMwzjWhAVsEKEqkxoTVFfVJIXhm245sODlkR2fnL6fTb4Q35Un765Q3sdsMQd2XsB07DOJoR2evp8sRiroAmW45Yk0tbbUBFIm9qQ23JSo6dldmgXQG64PY9chaFR+SNBG4mi+9zbBx7p1QhI3dNGUoQ9nqAbelyMDswHzAHBgUrDgMCGgQUFoO+t5167Wk/LjTD1iMPcko2Y10EFF2P2lFozA/2icmk38qnuYMqorN3AgIH0A==";

    /**
     * RSA通用加解密  公钥私钥
     */
    //云工厂公钥、私钥、密码
    public static String ygcpublic = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsJkaRgBXt7XQ8obBTw9NaZ/DoWVa/TnNc9RikV/VZLGhGRKSlExcSghewA2V6gP3C8rjUHoycrc1uX1Ce3pSPmHm7V4PKBGFKnwnBbgSTGS+xCXOrBomJ42L+QZP3xYyVnpPWSPy4uqjtVDZCCBTwC0+iJi/wqIr2d4LEgW1ObTpGU/zK4Fck+IqxDUzVE1pt5+yGGNcASTL/2j9kKmF35lRYuUjIw2uO4he3KoNWFCV7MBMcWw8BINyL4ZDPJpC6r7ywPvioJSJxBoK9BGDoBcRFf66aY9B1Lk+3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hOBR6irX3y/78FEbqyNUxBDxtDOuWWJ6DX/zd+AcE437FOv8rccYwpEIRoGIhcKr2Ez4HFdOip6h7i/0RCkdxUxAIPOx7fowKBgC3Q8Eju4ezwgbv/q/qsHMTbQXF4tzQ9HASNU/RhnEhs87zj+tRg3d/Esj4ygX3f/qq6b1cyvugiWHg3rO3ReTpkNaM+WjmwybGFBA+PJaYGRwUqkDe/8IbHjjHm+MNPZFl14/R2JEOzA43EMapFHdsqJpwnWZ3yMgBjOhKGORKJAoGAdCTUMNmzcaCb2xZK6+vHTMAU8JyEeDyG/w9cSJyIXlXPGDXGWqcG+xODSDlNxcYhNh2EK7jvSQCX9ap44iO/7C6y0jX7lqsJohmQwN39ngkWK8oL4WDoNlt4KsxKz9SMyrAQ5e+jFbzIk4V8WaV5Y08PTibnLhQe5/fhTIasVoUCgYB8m3tWCkdS0GAnSQZc4kOAXecxMXW54V1AblbkS8HF7qqlvlEOf2yUgR8GlFdeK4Hw82hf1UR1EgMIqoLVFLgqWEyzd9Ny3U5gaEKnNFkWXyc3QXvrdT8IIcYoDk1OSz4PQ0iaLNzsqAlC6Lt1Dyq05Q1QJT7s6LN1Y55LOwyP8g==";

    //网贷公钥、私钥、密码
    public static String wdPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmq4VOd7qIbyp4H3kNPOMlRLYaa+Zy8o4+kHJh7VWw3KSfpUcj63VgCPR6F6/SZrG4wOYiaiZzKa8P9A3qDK++jo/4Ah4Zk409lfNqoArd+OS+iFm3JIaciIHPfuPOvGy6B9vhc2o9B9pI8cwnOxoT4bFrNx0igwS/tM0OnBOQbll/BWvruX6tX/sx/FyGR18I5y3AlaIRsMU9H60ai5YXABxre3AHC5L6NuN5B7fqR6yUeHa1NyfK6suTz9QW+1qiHkilc+/FD7Nec4q8YvZBwNu1/5T8aQnLl+3QINmLRrxktGYSt8Glyxda3zizpvC84PIDZEXokDhkA7Q24I/7wIDAQAB";
    public static String wdPrivate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utzkHTsi9MnZkEYXA9unoXs1wKBgBXl3+97ftFZ3Xp+9k8zrg3BSoTh3nZTDZ/FpwrlgWxJiOWdiR2PhXptJasLsSj7ZE+HIfjA0/M2yHQxxTGVl+fgfueajPVpzt73hgI0a3rusXcv0ee75BqqP6AHmriRQEd/GUgHTeMDoQWwPwdAdpFtt7t9ztKVDB85z1ak7JZpAoGBALigHIonSzhG/IHppGpTWj8TN8L8yQbeNveZ46nd1Z8WXD/vRZ0CKY/CIIqfWxdGkpMDCBOxoksJ+1D7nrmr6c68NsN3Em0GHeMTBt9w6uD4XQluIneG90c50xiUtsunCHO5BTMvfZmgVIkZVJjkL2+WY4cdrvDj4ccmed80+HalAoGAH//524Y4aykIw/L6ORcPwXLA9b5gb2dx3zryvd6dn5J2fI8zXteT4eZ8nRDkts5OdrqGKYEp+H5fEtzLYDLTcSFc23ZcqFazWiO7DKG21v8gWp4i+VT5JBTcgBnopgAmxnHGV2IpvBjK8ECSpYvL7PKM8vhA4d1195ZIxxTm/ac=";


    //初始化密钥对(公钥和私钥)
    static {
        keyMap.put(PUBLIC_KEY, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArfJpaC4xQhnm9KUdXDlDcLxf1DfNxc9wfN3vpNANgSBYefnCnMra51GMp13VpvBzCMe2n9T64YMzVDq3T6yBbo76kkGGaJIZAKfnnKpzyhdT4LqG3DPtuHrjIGXJT4pSF/wvZddHbeQI2ubd4lbvY3NOwIiUxfon5HJ4lCUVnMx/v2smmfVKNHENASLB9LXWEmqtkSMWln9zLirJKJ0YeKw2QxjDVQePdNxyhDoYXV0NWMLLotrPepKH+/EtQobhqcv7YNppRenPa3C3xAauMfhjgGZcKmdFxvHDeKYRjdNc/3MYdgeNQZC8haerfB4wu1kyZtbqkSXh3meRlgI0QQIDAQAB");
        keyMap.put(PRIVATE_KEY, "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCt8mloLjFCGeb0pR1cOUN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YOO7Wx2ZJVJuQ3D1yY9gQo3k26niLZ1Ph17J7RgfV4y4OiLQIqnWRoGBqZMC48EDRxjIvIc0MpPiFM7OtWvunjuKUOyNW5P812Tac0e5ECERx/fPtxI1JcwGfE7jb+5gC32gIwKBgBbxlbn3esLU5Q2dLEmpTTjzjZVxxOOFnSrH1lA2orrpmopBa/sKOWSqGIK5a9YzSnFMNVkoYNRaWwTpssbjieLPeravcw3h/kR8fepFcrLYnCiRiR4enlEs6yPugO2Ik5uYwAH9eGNhhx0JUR/rIKZbnBAK36WWNApAzaDKWvaTAoGAacUU8Msa7NJvVxCgwnWWCBzOZITmsRoDK610OdlNEyuIF3zH0K/F3aqtX8FX/3JIYi1VqwMk9UGSGsL85+a6GhhjylZrLIzIgCW5MUeccla1WKN6qwFO/3wAghhAkvQuOUmVkJSBrUM0MMbvr27g8ptzxSE+9hViiQQ9axGx14A=");
    }

    public static Map<String, Object> getKeyMap() {
        return keyMap;
    }

    /**
     * 生成密钥对(公钥和私钥)
     *
     * @param keySize 密钥对指定长度
     * @return
     * @throws Exception
     */
    public static Map<String, Object> initKey(int keySize) throws Exception {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGen.initialize(keySize);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        keyMap.put(PUBLIC_KEY, Base64.encodeBase64String(publicKey.getEncoded()));
        keyMap.put(PRIVATE_KEY, Base64.encodeBase64String(privateKey.getEncoded()));
        return keyMap;
    }

    /**
     * 公钥加密
     *
     * @param data      源数据
     * @param publicKey 公钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static String encryptByPublicKey(byte[] data, String publicKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(publicKey.getBytes());
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        byte[] encrypt = encrypt(data, KeyFactory.getInstance(KEY_ALGORITHM), keyFactory.generatePublic(x509KeySpec));
        return Base64.encodeBase64String(encrypt);
    }

    /**
     * 私钥加密
     *
     * @param data       源数据
     * @param privateKey 私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(privateKey.getBytes());
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        return encrypt(data, keyFactory, privateK);
    }

    /**
     * 文件进行解密
     *
     * @param encryptedData 已加密数据
     * @param privateKey    私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static String decryptByPrivateKeyToFile(byte[] encryptedData, String privateKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(privateKey.getBytes());
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        byte[] decrypt = decrypt(encryptedData, keyFactory, keyFactory.generatePrivate(pkcs8KeySpec));
        return Base64.encodeBase64String(decrypt);
    }

    /**
     * 私钥解密
     *
     * @param encryptedData 已加密数据
     * @param privateKey    私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static String decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(privateKey.getBytes());
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        byte[] decrypt = decrypt(encryptedData, keyFactory, keyFactory.generatePrivate(pkcs8KeySpec));
        return new String(decrypt);
    }

    /**
     * 公钥解密
     *
     * @param encryptedData 已加密数据
     * @param publicKey     公钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(publicKey.getBytes());
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        return decrypt(encryptedData, keyFactory, publicK);

    }

    /**
     * 用私钥对信息生成数字签名
     *
     * @param data       已加密数据
     * @param privateKey 私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static String sign(byte[] data, String privateKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(privateKey.getBytes());
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Signature signature = Signature.getInstance(MD_5_WITH_RSA);
        signature.initSign(privateK);
        signature.update(data);
        return new String(Base64Utils.encode(signature.sign()));
    }

    /**
     * 校验数字签名
     *
     * @param data      已加密数据
     * @param publicKey 公钥(BASE64编码)
     * @param sign      数字签名
     * @return
     * @throws Exception
     */
    public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
        byte[] keyBytes = Base64Utils.decode(publicKey.getBytes());
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey publicK = keyFactory.generatePublic(keySpec);
        Signature signature = Signature.getInstance(MD_5_WITH_RSA);
        signature.initVerify(publicK);
        signature.update(data);
        return signature.verify(Base64Utils.decode(sign.getBytes()));
    }

    /**
     * 获取私钥
     *
     * @param keyMap 密钥对
     * @return
     * @throws Exception
     */
    public static String getPrivateKey(Map<String, Object> keyMap) {
        return (String) keyMap.get(PRIVATE_KEY);
    }

    /**
     * 获取公钥
     *
     * @param keyMap 密钥对
     * @return
     * @throws Exception
     */
    public static String getPublicKey(Map<String, Object> keyMap) {
        return (String) keyMap.get(PUBLIC_KEY);
    }

    /**
     * 解密公共方法
     */
    private static byte[] decrypt(byte[] data, KeyFactory keyFactory, Key key) throws Exception {
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, key);
        return encryptAndDecrypt(data, cipher, MAX_DECRYPT_BLOCK);
    }

    /**
     * 加密公共方法
     */
    private static byte[] encrypt(byte[] data, KeyFactory keyFactory, Key key) throws Exception {
        // 对数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return encryptAndDecrypt(data, cipher, MAX_ENCRYPT_BLOCK);
    }

    /**
     * 加密解密分段处理公共方法
     *
     * @param data
     * @param cipher
     * @param maxSize
     * @return
     * @throws Exception
     */
    private static byte[] encryptAndDecrypt(byte[] data, Cipher cipher, int maxSize) throws Exception {
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = ZERO;
        byte[] cache;
        int i = ZERO;
        // 对数据分段加密
        while (inputLen - offSet > ZERO) {
            if (inputLen - offSet > maxSize) {
                cache = cipher.doFinal(data, offSet, maxSize);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, ZERO, cache.length);
            i++;
            offSet = i * maxSize;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    /**
     * @方法描述: 通过秘钥文件路径地址获取字节二进制数据，然后调用网银AKS.RSA加解密
     * @方法名称: RSAUtil.getPrivateKeyBytes
     * @方法操作人: ext.fengcheng
     * @方法生成时间: 2022/11/21 17:02
     * @方法执行参数: [filePath]
     * @方法返回参数: byte[]（秘钥文件地址）
     * @方法产生异常:
     **/
    public static byte[] getPrivateKeyBytes(String filePath) throws IOException {
        log.debug("RSAUtil.getPrivateKeyBytes.filePath.{}", filePath);
        byte[] bytes = null;
        File file = new File(filePath);
        if(!file.exists() || !file.isFile() ){
            log.warn("RSAUtil.getPrivateKeyBytes.此秘钥文件不是一个文件，请检查服务器秘钥文件是否已上传！");
            return null;
        }
        bytes = Files.readAllBytes(Paths.get(file.getPath()));
        log.debug("RSAUtil.getPrivateKeyBytes.bytes.{}", bytes);
        return bytes;
    }

    /**
     * @方法描述: 获取网银AKS.RSA加解密对象
     * @方法名称: RSAUtil.getSignEnvelopServiceInfo
     * @方法操作人: ext.fengcheng
     * @方法生成时间: 2022/11/23 17:19
     * @方法执行参数: [customVersion]
     * @方法返回参数: com.wangyin.aks.security.sign.SignEnvelopService
     * @方法产生异常:
     **/
    public static SignEnvelopService getSignEnvelopServiceInfo(CustomVersionEnum customVersion){
        if(!signCasheMap.containsKey(customVersion)){
            signCasheMap.put(customVersion, new SignEnvelopServiceImpl());
        }
        return signCasheMap.get(customVersion);
    }


    /**
     * @方法描述: 网银AKS.RSA加密公共方法
     * @方法名称: RSAUtil.wyaksRsaEncrypt
     * @方法操作人: ext.fengcheng
     * @方法生成时间: 2022/11/18 20:52
     * @方法执行参数: [privateSecurity（私钥）, password（密码）, publicSecurity（公钥）, bizMsgObj（明文数据）]
     * @方法返回参数: java.lang.String
     * @方法产生异常:
     **/
    public static String wyaksRsaEncrypt(String privateSecurity, String password, String publicSecurity, String bizMsgObj, CustomVersionEnum customVersion) throws FilterException {
        log.info("RSAUtil.wyaksRsaEncrypt.开始.调用网银AKS.RSA加密公共方法");
        String ciphertext = "";
        try {
            if(StringUtils.isBlank(privateSecurity) || StringUtils.isBlank(password) || StringUtils.isBlank(publicSecurity) || StringUtils.isBlank(bizMsgObj)){
                log.warn("RSAUtil.wyaksRsaEncrypt.网银AKS.RSA加密公共方法执行失败，（私钥）, （密码）, （公钥）,（密文数据）有一个为空！");
                return null;
            }
            SignEnvelopService signEnvelopService = getSignEnvelopServiceInfo(customVersion);
            ciphertext = signEnvelopService.signEnvelop(privateSecurity, password, publicSecurity, bizMsgObj.getBytes());
        } catch (Exception e) {
            log.error("RSAUtil.wyaksRsaEncrypt.网银AKS.RSA加密公共方法执行异常.异常信息：{}", e.getMessage(), e);
            throw new FilterException(new ResourceCapability.AutoDefineResource(BizErrorCodeEnum.FAILURE_8.getCode(), BizErrorCodeEnum.FAILURE_8.getDesc()));
        }
        log.info("RSAUtil.wyaksRsaEncrypt.调用网银AKS.RSA加密公共方法.结束");
        return ciphertext;
    }

    /**
     * @方法描述: 网银AKS.RSA解密公共方法
     * @方法名称: RSAUtil.wyaksRsaDecrypt
     * @方法操作人: ext.fengcheng
     * @方法生成时间: 2022/11/18 20:52
     * @方法执行参数: [privateSecurity（私钥）, password（密码）, ciphertext（密文数据）]
     * @方法返回参数: java.lang.String
     * @方法产生异常:
     **/
    public static byte[] wyaksRsaDecrypt(String privateSecurity, String password, String ciphertext, CustomVersionEnum customVersion) throws FilterException {
        byte[] plaintextArray = null;
        try {
            if(StringUtils.isBlank(privateSecurity) || StringUtils.isBlank(password) || StringUtils.isBlank(ciphertext)){
                log.warn("RSAUtil.wyaksRsaEncrypt.网银AKS.RSA加密公共方法执行失败，（私钥）, （密码）, （密文数据）有一个为空！");
                return null;
            }
            SignEnvelopService signEnvelopService = getSignEnvelopServiceInfo(customVersion);
            plaintextArray = signEnvelopService.verifyEnvelop(privateSecurity, password, ciphertext);
        } catch (Exception e) {
            log.error("RSAUtil.wyaksRsaEncrypt.网银AKS.RSA加密公共方法执行异常.异常信息：{}", e.getMessage(), e);
            throw new FilterException(new ResourceCapability.AutoDefineResource(BizErrorCodeEnum.FAILURE_8.getCode(), BizErrorCodeEnum.FAILURE_8.getDesc()));
        }
        log.info("RSAUtil.wyaksRsaDecrypt.调用网银AKS.RSA解密公共方法.结束");
        return plaintextArray;
    }

    public static void wyaksRSA() throws FilterException{
        String bizMsgObj = "{\"channelCustomerCode\":\"PCID0260000022201181018000002120\",\"lendInfo\":{\"bankName\":\"黑龙江佳木斯郊区合作社\",\"bankUnionCode\":\"HLJNX\",\"baseRate\":0.038,\"baseRateType\":\"LPR\",\"beginRepaymentDate\":\"2022-03-2817:20:35\",\"cardName\":\"成顺\",\"cardNo\":\"6122988320342234\",\"compoundRate\":0}}";
        System.out.println("网贷使用云工厂公钥RSA加密======>开始");
        String encrypt = wyaksRsaEncrypt(wdprivateSecurity, saltwd, ygcpublicSecurity, bizMsgObj, CustomVersionEnum.YGC);
        System.out.println("网贷使用云工厂公钥RSA加密========密文：" + encrypt);
        System.out.println();
        encrypt = "MIIKKAYJKoZIhvcNAQcDoIIKGTCCChUCAQAxggEVMIIBEQIBADB6MG0xCzAJBgNVBAYTAmNuMQswCQYDVQQIDAJjZDELMAkGA1UEBwwCY2QxCzAJBgNVBAoMAnBwMQswCQYDVQQLDAJwcDELMAkGA1UEAwwCcHAxHTAbBgkqhkiG9w0BCQEWDmxpeHV3ZXVAamQuY29tAgkAnFpDmG7Tck8wDQYJKoZIhvcNAQEBBQAEgYCLZvgtRUfQyJZ/vP/JTApsyPoOmkClc9+BMLR3J7SwdcSyz71xK02btfDyU84gcdo04OLR+b6LWAa0Poevcxz+pmV0bg4kDd4yySJ1OR8DYP9iEoUJmJKOc1fA6aZ05Y8rNcseKuNtc5BmKpYCVvMNGRbKeS/J+iHo2wCLsqZZ3TCCCPUGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIUxgtxJrD5leAggjQzrDl38IYt2bSmhPtEwwNJJXzEol9GZRLahoeprjxR56xmSovNQzNv3RVo2Ghja0rI/4qqo6dfIaf9vbKOj/dkSCs0q5v4p+xj36kiJS7VC0esalknm3a9QPxgDgaSVPNNRou38YL8TQvcBrNGBP7I23zgnvufsGCAdKKRoSFhpTbu9ZQ+vkjouH1l4lgnIX5GMx3pbL5XW2IWk3qvQIQQaDqNoqhpf0zJ0O8EBZWxKVMl7c9Iwxi1hXRhi7wLsJT0v1l09LgcvVafd/pJ0o9tSdiz2trkbDweAu+yf7yokoMMiOGl1fKrbFwl52TAjrblM9QuD02jsYaEYQ95An1Dt5RlEXEFD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bn4+MWPEhYp5/darSDfREMDGPjk436BQgywT65r+WvEbNSJIHDqavpV/tRMQY/6fH6u+i+ndSvJ0j/5IAIMoROHXTt+b/RMlouytu/HcgN9i2AIP7AtMfOqgaZeZyiXKbkGEGhb2YT6P1/rHVKrundFgE+s24i0B/Jv1JoIjI5wL6JbIoV2gWV0wg8NJ6x5F/6rRc/tloWU+2Gov4JTdessZ3gEzu5QXySndmhkvTFbPy89ar583Bz+tJw7LW4gbb2OZ7aYViSLu6KYVaFKWzpCbul8t+ocxzcxboY0CMS1x0XH6SXee/pz/yjUTl0yc3JWTNQXCFZ85q5QjIsGa/VUShblNikCbgE3MlBE5NIii2idW4xU/trCtXU3vsxRWQR/Ma3pqXRTnHjLG6tnz/p9Yu/M+a8ACzAvvMz4FVbBvhEyl7MwBxRkhY0mp87+98KCvGVoMTK/vVxQsAl6R3p0Lr+39WqV3S50CrNHExTd9pBxkQRpPQLOc6OBwFKPo9F58";
        System.out.println("云工厂使用私钥RSA解密======>开始");
        System.out.println("云工厂使用私钥RSA解密========明文：" + new String(wyaksRsaDecrypt(ygcprivateSecurity, saltygc, encrypt, CustomVersionEnum.YGC)));
        System.out.println();

        System.out.println("云工厂使用网贷公钥RSA加密======>开始");
        String decrypt = wyaksRsaEncrypt(ygcprivateSecurity, saltygc, wdpublicSecurity, bizMsgObj, CustomVersionEnum.YGC);
        System.out.println("云工厂使用网贷公钥RSA加密========密文：" + decrypt);
        System.out.println();

        System.out.println("网贷使用私钥RSA解密======>开始");
        System.out.println("网贷使用私钥RSA解密========明文：" + new String(wyaksRsaDecrypt(wdprivateSecurity, saltwd, decrypt, CustomVersionEnum.YGC)));
    }

    /**
     * 公共RSA通用加解密
     * @throws Exception
     */
    public static void publicRsaEncryptAdnDecrypt() throws Exception {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            str.append("你个噔噔儿，");
        }

        //网贷加密---云工厂解密
        //网贷使用云工厂公钥加密
        String encryptTtext = RSAUtil.encryptByPublicKey(str.toString().getBytes(), ygcpublic);
        //公钥加密后，开始用网贷自己的私钥生成签名
        String sign = RSAUtil.sign(encryptTtext.getBytes(), wdPrivateKey);

        //云工厂开始解密
        //解密前，先用云工厂自己的公钥进行验证签名
        boolean verify = RSAUtil.verify(encryptTtext.getBytes(), wdPublicKey, sign);
        //验证签名成功，使用云工厂自己的私钥进行解密
        String decryptText = RSAUtil.decryptByPrivateKey(Base64.decodeBase64(encryptTtext), ygcprivate);

        System.out.println();
        System.out.println("****** 网贷加密---云工厂解密 start ******");
        System.out.println("加密后通过秘钥生成签名 ：" + sign);
        System.out.println();
        System.out.println("解密前先通过公钥进行验证签名 ：" + verify);
        System.out.println("解密后 ：" + decryptText);
        System.out.println("****** 公钥加密 私钥解密 end ******");
        System.out.println();
        System.out.println("开始进行私钥加密，公钥解密");
        System.out.println();
        System.out.println();
        System.out.println();


        //云工厂加密---网贷解密
        //云工厂使用网贷公钥加密
        encryptTtext = RSAUtil.encryptByPublicKey(str.toString().getBytes(), wdPublicKey);
        //公钥加密后，开始用云工厂自己的私钥生成签名
        sign = RSAUtil.sign(encryptTtext.getBytes(), ygcprivate);

        //网贷开始解密
        //解密前，先用网贷自己的公钥进行验证签名
        verify = RSAUtil.verify(encryptTtext.getBytes(), ygcpublic, sign);
        //验证签名成功，使用网贷自己的私钥进行解密
        decryptText = RSAUtil.decryptByPrivateKey(Base64.decodeBase64(encryptTtext), wdPrivateKey);

        System.out.println();
        System.out.println("****** 云工厂加密---网贷解密 start ******");
        System.out.println("加密后通过秘钥生成签名 ：" + sign);
        System.out.println();
        System.out.println("解密前先通过公钥进行验证签名 ：" + verify);
        System.out.println("解密后 ：" + decryptText);
        System.out.println("****** 公钥加密 私钥解密 end ******");
        System.out.println();
        System.out.println("开始进行私钥加密，公钥解密");
    }
}
